<html>
<head><meta charset="utf-8"><title>Panic in embedded arm system: LPC55 · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20in.20embedded.20arm.20system.3A.20LPC55.html">Panic in embedded arm system: LPC55</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="230849901"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20in%20embedded%20arm%20system%3A%20LPC55/near/230849901" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20in.20embedded.20arm.20system.3A.20LPC55.html#230849901">(Mar 18 2021 at 11:52)</a>:</h4>
<p>Hi all, I am having trouble debugging a panic (BusFault) that i'm getting. My board is LPC55S6, a dual ARM cortex M-33 board.</p>
<p>The busfault seems to be a 'precise data bus error'.</p>
<div class="codehilite"><pre><span></span><code>&gt;&gt;&gt; x/1bt 0xE000ED29  # BFSR
0xe000ed29:     10000010
&gt;&gt;&gt; x/wx 0xE000ED34  # BFAR
0xe000ed34:     0x00000004
</code></pre></div>
<p>The violation seems to be caused by the access to the memory 0x4</p>
<div class="codehilite"><pre><span></span><code> 0x000024ae  harsark::utils::arch::PendSV_1::{{closure}}+562 mov    r10, r6
 0x000024b0  harsark::utils::arch::PendSV_1::{{closure}}+564 mov    r11, r7
 0x000024b2  harsark::utils::arch::PendSV_1::{{closure}}+566 ldmia    r0!, {r4, r5, r6, r7}
 0x000024b4  harsark::utils::arch::PendSV_1::{{closure}}+568 msr    PSP, r0
 =&gt; 0x000024b8  harsark::utils::arch::PendSV_1::{{closure}}+572 str    r3, [r5, #4]
 0x000024ba  harsark::utils::arch::PendSV_1::{{closure}}+574 ands.w    r0, r8, #1
 0x000024be  harsark::utils::arch::PendSV_1::{{closure}}+578 dmb    sy
</code></pre></div>
<div class="codehilite"><pre><span></span><code>─── Registers ────────────────────
           r0 0x200212d8             r1 0x20020158           r2 0x200234f8           r3 0x00000002
           r4 0x00000000             r5 0x00000000           r6 0x00000000           r7 0x00000000
           r8 0x00000000             r9 0x00000000          r10 0x00000000          r11 0x00000000
          r12 0x00000fa7             sp 0x2001ff90           lr 0x00002273           pc 0x000024b8
         xpsr 0x0900000e            msp 0x2001ff70          psp 0x200212d8      primask 0x00000001
</code></pre></div>
<p>The code surrounding the violation is provided above. I've marked PC with <code>=&gt;</code>.</p>
<p>The source code line that gdb shows that is associated with the instruction is </p>
<div class="codehilite"><pre><span></span><code> 256                  handler.curr_tid = next_tid;
</code></pre></div>
<p>While I know the cause of the violation, I have no idea how or why this instruction is getting generated. It would be great if someone could guide me towards finding the root cause of this issue. Please let me know if more data (source or other things) is needed.</p>



<a name="231040399"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20in%20embedded%20arm%20system%3A%20LPC55/near/231040399" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oliver <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20in.20embedded.20arm.20system.3A.20LPC55.html#231040399">(Mar 19 2021 at 15:22)</a>:</h4>
<p>the Rust embedded working group is largely on the Matrix chat platform</p>



<a name="231593505"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20in%20embedded%20arm%20system%3A%20LPC55/near/231593505" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20in.20embedded.20arm.20system.3A.20LPC55.html#231593505">(Mar 24 2021 at 07:33)</a>:</h4>
<p>Thanks, I've asked there as well. </p>
<p>BTW, I found the root issue for this but I'm still unsure how to fix it. Maybe someone here that knows ARM can help me as well (?)</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">load_context</span><span class="p">(</span><span class="n">task_stack</span>: <span class="kp">&amp;</span><span class="nc">TaskControlBlock</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="fm">asm!</span><span class="p">(</span><span class="w"></span>
<span class="w">            </span><span class="s">"cpsid  i"</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="s">"mov    r1, {0}"</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="s">"@ldr   r1, [r2]"</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="s">"@ldr   r1, [r1]"</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="s">"ldr    r0, [r1]"</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="s">"ldmia  r0!,{{r4-r7}}"</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="s">"mov    r8, r4"</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="s">"mov    r9, r5"</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="s">"mov    r10, r6"</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="s">"mov    r11, r7"</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="s">"ldmia  r0!,{{r4-r7}}"</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="s">"msr    psp, r0"</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="k">in</span><span class="p">(</span><span class="n">reg</span><span class="p">)</span><span class="w"> </span><span class="n">task_stack</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">out</span><span class="p">(</span><span class="s">"r0"</span><span class="p">)</span><span class="w"> </span><span class="n">_</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">out</span><span class="p">(</span><span class="s">"r1"</span><span class="p">)</span><span class="w"> </span><span class="n">_</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>The above is my load_context function which is used which context switching. After this is executed, the r4, r5, r6, r7 values are changed. In debug mode, the code that is executed after this call does not use any of these registers but in release mode it does.</p>
<p>What's the normal technique followed during a context switch? Can someone provide me with some pointers on how to fix this?</p>



<a name="231614566"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20in%20embedded%20arm%20system%3A%20LPC55/near/231614566" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20in.20embedded.20arm.20system.3A.20LPC55.html#231614566">(Mar 24 2021 at 10:54)</a>:</h4>
<p>You clobber the registers that your inline asm block has no permission to use.</p>



<a name="231614655"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20in%20embedded%20arm%20system%3A%20LPC55/near/231614655" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20in.20embedded.20arm.20system.3A.20LPC55.html#231614655">(Mar 24 2021 at 10:55)</a>:</h4>
<p>Only registers you may use here is {0}, r0 and r1 as specified in the constraints.</p>



<a name="231648396"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20in%20embedded%20arm%20system%3A%20LPC55/near/231648396" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20in.20embedded.20arm.20system.3A.20LPC55.html#231648396">(Mar 24 2021 at 14:59)</a>:</h4>
<p><span class="user-mention silent" data-user-id="123586">nagisa</span> <a href="#narrow/stream/122651-general/topic/Panic.20in.20embedded.20arm.20system.3A.20LPC55/near/231614566">said</a>:</p>
<blockquote>
<p>You clobber the registers that your inline asm block has no permission to use.</p>
</blockquote>
<p>But it's a context switch. I'm doing it intentionally (more or less).</p>



<a name="231648602"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20in%20embedded%20arm%20system%3A%20LPC55/near/231648602" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vn-ki <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20in.20embedded.20arm.20system.3A.20LPC55.html#231648602">(Mar 24 2021 at 15:00)</a>:</h4>
<p>Is there any way I can tell the compiler to not use some registers for code gen?</p>



<a name="231648806"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20in%20embedded%20arm%20system%3A%20LPC55/near/231648806" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20in.20embedded.20arm.20system.3A.20LPC55.html#231648806">(Mar 24 2021 at 15:01)</a>:</h4>
<p>Then the restoring of registers on context switch is not correct, I guess.</p>



<a name="231648829"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20in%20embedded%20arm%20system%3A%20LPC55/near/231648829" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20in.20embedded.20arm.20system.3A.20LPC55.html#231648829">(Mar 24 2021 at 15:01)</a>:</h4>
<p>you need to do the register restore <em>and</em> control transfer to the other context in a single asm block</p>



<a name="231648924"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20in%20embedded%20arm%20system%3A%20LPC55/near/231648924" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20in.20embedded.20arm.20system.3A.20LPC55.html#231648924">(Mar 24 2021 at 15:01)</a>:</h4>
<p>I'm not aware of any other way to do a UB-free context switch</p>



<a name="231649619"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20in%20embedded%20arm%20system%3A%20LPC55/near/231649619" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20in.20embedded.20arm.20system.3A.20LPC55.html#231649619">(Mar 24 2021 at 15:05)</a>:</h4>
<p>IOW, it is actually fine to use whatever registers within the same inline assembly block however you want, as long as by the end of that assembly block the register values are the same as at the beginning of it.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>